<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Admin\Goods;
use App\Models\Admin\GoodsBrand;
use App\Models\Admin\GoodsClass;
use App\Models\Admin\GoodsSpec;
use App\Services\GoodsClassService;
use App\Services\GoodsService;
use Illuminate\Http\Request;
use App\Services\UploadService;
use App\Models\Admin\GoodsAttr;


class GoodsController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $data = Goods::paginate($this->pagesize);
        return view('admin.admin_goods.index',compact('data'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        $view_data = [];
        $goods_class_service = new GoodsClassService();
        $model = GoodsClass::all();
        $view_data['classesData'] = $goods_class_service->get_tree_classes($model);
        $view_data['brandData'] = GoodsBrand::all();
        //属性
        $view_data['attrData'] = GoodsAttr::all();
        return view('admin.admin_goods.add',$view_data);

    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request, GoodsService $goodsservice)
    {
        //
         $info = $goodsservice->add();
         if($info['status']==-1){
             return ['status'=>-1,'msg'=>'失败'];
         }
        return ['status'=>200,'msg'=>'成功'];
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    //上传商品图片
    public function goods_image_upload(Request $request,UploadService $upload_service)
    {
        $pic = '';
        if($request->hasFile('file')){
            //参数2 配置的节点名称
            $ret = $request->file('file')->store('','goods_class_thumb');
            $pic = '/uploads/goods_class_thumb/'.$ret;
        }
        return ['status'=>200,'url'=>$pic];
//        $upload_service->goods_class();
    }
}
